
function soapWrapFunc(bdStr){
    // 封装SOAP头尾
    var soapHead = '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice.zjusc.edu.cn/"><env:Body>', soapTail = '</env:Body></env:Envelope>';
    return soapHead + bdStr + soapTail;
}
//删除考试，通过ID
function deleteAdminById(id){
	//验证报文
    var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';
	
    var soapBody = '<web:DeleteOnlineRegisterAdminInfo>'+userinfo+'<id>' + id + '</id></web:DeleteOnlineRegisterAdminInfo>';
   
    $.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/OnlineRegisterAdminService',
        data: soapWrapFunc(soapBody),
        dataType: 'xml',
        success: function(xml){
        	
            alert($(xml).find('return').text());
            loadTeacherData();
        },
        beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
        	 var xmlObj = $(x.responseText);
             if (xmlObj.find('faultstring').text() == 'oops') {
                 alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                 return;
             }
             else {
                 alert('error:' + x.responseText);
             }
        },
        complete: function(x){
        
        }
    });
}
//加载考试数据
function loadTeacherData(){
	 var getExamStrBody = '<web:GetAllOnlineRegisterAdminInfo></web:GetAllOnlineRegisterAdminInfo>';
    
    $.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/OnlineRegisterAdminService',
        data: soapWrapFunc(getExamStrBody),
        dataType: 'xml',
        success: function(xml){
            var tableHtml = '<tbody>';
            $.each($(xml).find('item'), function(i, n){
                tableHtml += (i % 2 == 0) ? '<tr>' : '<tr class="odd">';
                tableHtml += '<td>' + $(this).find('id').text() + '</td>';
                tableHtml += '<td>' + $(this).find('username').text() + '</td>';
                tableHtml += '<td>●●●●●●</td>';
                tableHtml += '<td><a href="javascript:void(0);" class="edit" >编辑</a><a href="javascript:void(0);" class="delete" >删除</a></td>';
                tableHtml += '</tr>';
            });
            tableHtml += '</tbody>';
            $('#examInfoTable').find('tbody').remove().end().append(tableHtml);
            //编辑按钮
            $('#examInfoTable .edit').click(function(){
				$('#maskLayer').height($('#container').height()).show();
                $('#examWnd').show();
                $('#wndTitle span').text('编辑用户信息');
                // 是编辑信息
                $.editMode = 1;
				var tdList=$(this).parents('tr').find('td');
                $.userId = tdList.eq(0).text();
				
				$('#username').val(tdList.eq(1).text());
				$('#password').val('391732016');	
				$('#passwordAgain').val('391732016');
			
            });
            //删除按钮
            $('#examInfoTable .delete').click(function(){
            	var tdList=$(this).parents('tr').find('td');
            	$.userId = tdList.eq(0).text();
                deleteAdminById($.userId);
            });
        },
        beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
            alert('error:' + x.responseText);
        },
        complete: function(x){
        
        }
    });
}

$(function(){
	$.editMode=0;
    loadTeacherData();
    $("#password").attr("disabled",true);
	$("#passwordAgain").attr("disabled",true);
    //勾选按钮
    $("#teachercontrol").click(function(){
    	if($("#teachercontrol").attr("checked")==("checked")){
        	$("#password").attr("disabled",true);
        	$("#passwordAgain").attr("disabled",true);
        }else{
        	$("#password").attr("disabled",false);
        	$("#passwordAgain").attr("disabled",false);
        }
    });
    
});
/*
 *
 *
 */
$(window).load(function(){
	//拖拽绑定
	$('#examWnd').dragDrop({ focuEle: '#wndTitle' });
				
    $('.timezone').datePicker({
        startDate: '2000-01-01',
        endDate: '2100-01-01',
        clickInput: true,
        verticalOffset: 35
    });
    $('#closeBtn').click(function(){
        $('#maskLayer').hide();
        $('#examWnd').find('input[type="text"]').val('').end().hide();
    });
    // 新增管理员
    $('#newButton').click(function(){
        $('#maskLayer').height($('#container').height()).show();
        $('#examWnd').show();
        $('#wndTitle span').text('新增用户');
        // 不是编辑考试，是新增考试
        $.editMode = 0;
        $('#password').val("");
        $('#passwordAgain').val("");
    });
    // 文本框获得焦点的时候，显示TIP
    $('#examWnd input[type="text"]').focusin(function(){
        $(this).parent().prev('.tip').show();
    }).focusout(function(){
        $('#examWnd .tip').hide();
    });
    $('#saveButton').click(function(){
    	//验证报文
        var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';
        
    	if($('#password').val()!=$('#passwordAgain').val()){
    		alert("对不起，两次输入的密码不一致");
    	}else {
        var  username = $.trim($('#username').val()), password = $.trim($('#password').val());
	
        //durationValue = $.trim($('#inDuration').val()), subjectValue = $.trim($('#inSubjectName').val()), noteValue = $.trim($('#inNote').val());
        //var hourValue = $.trim($('#inExamTime_Hour').val()), minValue = $.trim($('#inExamTime_Min').val()), secValue = $.trim($('#inExamTime_Sec').val());
        /*
         * 这里要进行输入验证
         * 由大家完成
         * 正则或其他验证等等
         * 
         * 
         * 
         * 
         * 
         * 
         */
       
        var idValue, soapFuncName;
        //新增考试
        if ($.editMode == 0) {
        	
        	if(password=="391732016"){
        		alert("对不起，请不要使用'391732016'作为密码");
        	}else{
            soapFuncName = 'NewOnlineRegisterAdminInfo';
            idValue = '';
            var sendBody = '<web:' + soapFuncName + '>'+userinfo+'<adminEntry>' +
            '<username>' +
            username +
            '</username>' +
            '<password>' +
            password +
            '</password>' +
            
            '</adminEntry></web:' +
            soapFuncName +
            '>';
            $.ajax({
                type: "POST",
                contentType: "text/xml",
                url: '../../Service/OnlineRegisterAdminService',
                data: soapWrapFunc(sendBody),
                dataType: 'xml',
                success: function(xml){
                    alert($(xml).find('return').text());
                    loadTeacherData();
                },
                beforeSend: function(xhr){
                    xhr.setRequestHeader("SOAPAction", "\"\"");
                },
                error: function(x, e){
                	var xmlObj = $(x.responseText);
                    if (xmlObj.find('faultstring').text() == 'oops') {
                        alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                        return;
                    }
                    else {
                        alert('error:' + x.responseText);
                    }
                },
                complete: function(x){
                
                }
            });
        }
        }
        else {
            //修改考试，需要多发送一个考试ID信息
            soapFuncName = 'ModifyOnlineRegisterAdminInfo';
            idValue = $.editID;
            
            //修改用户信息
            var sendBody = '<web:' + soapFuncName + '>'+userinfo+'<adminEntry>' +
            '<id>' +
            $.userId +
            '</id>' +
            '<username>' +
            username +
            '</username>' +
            '<password>' +
            password +
            '</password>' +
            
            '</adminEntry></web:' +
            soapFuncName +
            '>';
            $.ajax({
                type: "POST",
                contentType: "text/xml",
                url: '../../Service/OnlineRegisterAdminService',
                data: soapWrapFunc(sendBody),
                dataType: 'xml',
                success: function(xml){
                    alert($(xml).find('return').text());
                    loadTeacherData();
                },
                beforeSend: function(xhr){
                    xhr.setRequestHeader("SOAPAction", "\"\"");
                },
                error: function(x, e){
                	var xmlObj = $(x.responseText);
                    if (xmlObj.find('faultstring').text() == 'oops') {
                        alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                        return;
                    }
                    else {
                        alert('error:' + x.responseText);
                    }
                },
                complete: function(x){
                
                }
            });
        }
    	
    		}
    });
});
